package npu.zunsql.xwz;

import java.awt.SystemTray;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;



public class SlowQueryJourney {
	//慢查询最小时间 单位ms
	private static double slowQueryTime = 1000;
	//是否采用全表扫描即慢查询	
	private static boolean AllisSlowQuery = false;
	//慢查询日志文件保存位置
	private static String addr = "C:/WorkSpace/test0.txt";
	
	//测试
	public static void main(String args[]) throws IOException, InterruptedException {
		//如何添加时间
		//long begintime = System.currentTimeMillis();
		//Thread.sleep(500);
		//long endtime = System.currentTimeMillis();
		
		
		addToQueryJourney(null, null, null, 200, null);
		
		open();
//		clear();
	}
	
	//打开慢查询日志    
	public static void open() throws IOException {
		FileReader fileReader = new FileReader(addr);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		String str;
		System.out.println("慢查询日志内容如下：");
		//bufferedReader.readLine()是读取文件的一行，如果有多行，会逐行读取。
		//当每一行不为空时则把内容打印到控制台中，也可以存到写入的流中，把内容写到文本里
		while((str = bufferedReader.readLine())!= null) {
			System.out.println(str);
		}
		//关闭文件流
		fileReader.close();
	}
	
	//向慢查询日志添加SQL记录 
	//包括：具体查询时间、SQL语句本体、查询时间
	//Time: 执行语句的时间：2020-11-14 12:00:01
	//User: 查询语句用户
	//Query_time: 查询花费时间
	//database:查询语句相关的数据库
	//query:查询语句内容
	public static void addToQueryJourney(String query, String time, String user, long Query_time, String database_name) throws FileNotFoundException {	
		//以addr创建一个文件
		File file = new File(addr);
		if(file.exists()) {
			System.out.println("文件存在");
		}
		else {
			try {
				file.createNewFile();
				System.out.println("文件创建成功");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		FileOutputStream out = new FileOutputStream(addr,true);
		//文件输入流 用来向文件中输入东西
		PrintStream p = new PrintStream(out);
		
		p.println("Time:"+time);
		p.println("database:"+database_name);
		p.println("User:"+user);
		p.println("query:"+query);
		p.println("queryTime:"+Query_time+"ms");
		
		p.println("---------------------------------------");
		System.out.println("写入成功");
	}
	
	//清空慢查询日志
	public static void clear() throws IOException {
		FileWriter fw = new FileWriter(addr);
		fw.write("");
		System.out.println("文件已清空");
	}
	
	//设置慢查询日志的相关参数
	public static void set(double slowQueryTime1,boolean AllisSlowQuery1, String addr1) {
		slowQueryTime = slowQueryTime1;
		AllisSlowQuery = AllisSlowQuery1;
		addr = addr1;
	}
	
	protected static double getSlowQueryTime() {
		return slowQueryTime;
	}
	
	protected static void setSlowQueryTime(double slowQueryTime) {
		SlowQueryJourney.slowQueryTime = slowQueryTime;
	}
	
	protected static boolean isAllisSlowQuery() {
		return AllisSlowQuery;
	}

	protected static void setAllisSlowQuery(boolean allisSlowQuery) {
		AllisSlowQuery = allisSlowQuery;
	}
	
	protected static String getAddr() {
		return addr;
	}

	protected static void setAddr(String addr) {
		SlowQueryJourney.addr = addr;
	}
}
